<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <form name="form">
      <p>
        姓名
        <input type="text" name="name" value="jack" />
      </p>
      <p>
        附件
        <input type="file" name="thingfile" />
      </p>
      <p>
        <button type="button" name="saveBtn">保存</button>
      </p>
    </form>
    <script>
      const form = document.form;

      form.saveBtn.onclick = function () {
        //收集表单信息
        const fd = new FormData(form);

        // 将数据发送服务器
        const xhr = new XMLHttpRequest();
        xhr.open("POST", "http://127.0.0.1:3000/upload");

        xhr.onload = function () {
          console.log(xhr.response);
        };
        // multipart/form-data;
        xhr.send(fd);
      };
    </script>
  </body>
</html>
